package com.qingcheng.service.impl;

import com.alibaba.fastjson.JSON;
import com.qingcheng.pojo.order.OrderItem;
import com.qingcheng.service.goods.StockBackService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 从队列中拉取消息,并存入数据库
 */
public class BackMessageConsumer implements MessageListener {

    @Autowired
    private StockBackService stockBackService;

    public void onMessage(Message message) {

        try {
            //获取消息
            String jsonString = new String(message.getBody());
            //将字符串转换为集合
            List<OrderItem> orderItemList = JSON.parseArray(jsonString, OrderItem.class);
            stockBackService.addList(orderItemList); //添加到回滚表中等待回滚
        } catch (Exception e) {
            e.printStackTrace();
            //记录日志，人工进行修改库存
        }

    }
}
